//
//  UIImageView+SEGWebCache.m
//  Commom
//
//  Created by Clavis on 2020/10/23.
//

#import "UIImageView+SEGWebCache.h"
#import "objc/runtime.h"
#import "UIView+WebCacheOperation.h"
#import "UIView+WebCache.h"

@implementation UIImageView (SEGWebCache)

- (void)setImageWithURL:(nullable NSURL *)url {
    [self setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
}

- (void)setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder {
    [self setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
}

- (void)setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
    [self setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
}

- (void)setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options context:(nullable SDWebImageContext *)context {
    [self setImageWithURL:url placeholderImage:placeholder options:options context:context progress:nil completed:nil];
}

- (void)setImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
    [self setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
}

- (void)setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
    [self setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
}

- (void)setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
    [self setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
}

- (void)setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDImageLoaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
    [self setImageWithURL:url placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
}

- (void)setImageWithURL:(nullable NSURL *)url
          placeholderImage:(nullable UIImage *)placeholder
                   options:(SDWebImageOptions)options
                   context:(nullable SDWebImageContext *)context
                  progress:(nullable SDImageLoaderProgressBlock)progressBlock
                 completed:(nullable SDExternalCompletionBlock)completedBlock {
    [self sd_internalSetImageWithURL:url
                    placeholderImage:placeholder
                             options:options
                             context:context
                       setImageBlock:nil
                            progress:progressBlock
                           completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
                               if (completedBlock) {
                                   completedBlock(image, error, cacheType, imageURL);
                               }
                           }];
}

#pragma mark 图片缓存存取与读取
+ (NSData*)saveServiceImage:(NSURL*)url
{
    
    NSArray* fileArray = [[url absoluteString] componentsSeparatedByString:@"/"];
    NSString* fileName = [fileArray objectAtIndex:[fileArray count] - 1];
    
    //取出document路径
    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", filePath, fileName]]) {
        NSData* data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", filePath, fileName]];
        return data;
    }
    else {
        
        NSData* data = [NSData dataWithContentsOfURL:url];
        
        //保存数据
        if ([data writeToFile:[NSString stringWithFormat:@"%@/%@", filePath, fileName] atomically:YES]) {
        }
        
        return data;
    }
}

+ (NSData*)saveServiceImage:(NSURL*)url isSmall:(BOOL)isSmall
{
    //取出文件名
    NSArray* fileArray = [[url absoluteString] componentsSeparatedByString:@"/"];
    NSString* fileName = [fileArray objectAtIndex:[fileArray count] - 1];
    
    //取出document路径
    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSFileManager* fileManager = [NSFileManager defaultManager];
    
    if (isSmall) {
        if ([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@/small", filePath, fileName]]) {
            
            NSData* data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/small", filePath, fileName]];
            return data;
        }
        else {
            NSData* data = [NSData dataWithContentsOfURL:url];
            [data writeToFile:[NSString stringWithFormat:@"%@/%@/small", filePath, fileName] atomically:YES];
            return data;
        }
    }
    else {
        if ([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@/big", filePath, fileName]]) {
            
            NSData* data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/big", filePath, fileName]];
            return data;
        }
        else {
            NSData* data = [NSData dataWithContentsOfURL:url];
            [data writeToFile:[NSString stringWithFormat:@"%@/%@/big", filePath, fileName] atomically:YES];
            return data;
        }
    }
}

+ (NSData*)saveServiceImage:(NSURL*)url imageView:(UIImageView*)imageView
{
    NSArray* fileArray = [[url absoluteString] componentsSeparatedByString:@"/"];
    NSString* fileName = [fileArray objectAtIndex:[fileArray count] - 1];
    
    //取出document路径
    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", filePath, fileName]]) {
        
        NSData* data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", filePath, fileName]];
        
        [imageView setImage:[UIImage imageWithData:data]];
        
        return data;
    }
    else {
        if (url && [url.absoluteString rangeOfString:@"null"].location != NSNotFound) {
            return nil;
        }
        NSData* data = [NSData dataWithContentsOfURL:url];
        //保存数据
        if ([data writeToFile:[NSString stringWithFormat:@"%@/%@", filePath, fileName] atomically:YES]) {
            
            [imageView setImage:[UIImage imageWithData:data]];
        }
        
        return data;
    }
}

+(NSData*)saveServiceImage:(NSURL*)url aImageView:(UIImageView*)aImageView isSmall:(BOOL)isSmall
{
    return [self saveServiceImage:url imageView:aImageView];
}

//压缩图片到指定大小
+ (UIImage*)scaleImage:(UIImage*)aImage scaledToSize:(CGSize)aSize
{
    UIGraphicsBeginImageContext(aSize);
    [aImage drawInRect:CGRectMake(0, 0, aSize.width, aSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end
